<template>
  <div class="root">
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="code" label="编号" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="status" sortable label="人数" />
      <el-table-column prop="ares" label="面积/m" />
    </el-table>
    <div style="margin-top: 20px; display: flex; justify-content: flex-end">
      <el-pagination background layout="prev, pager, next, " :total="100" />
    </div>

    <el-dialog :title="dialogTittle" v-model="dialogVisible">
      <el-form
        v-if="dialogVisible"
        ref="ruleFormRef"
        :model="submitFormdata"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item label="收纳时间" prop="time">
          <el-date-picker
            v-model="submitFormdata.time"
            type="date"
            placeholder="选择时间"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="submitFormdata.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="使用状态" prop="status">
          <el-input v-model="submitFormdata.status" autocomplete="off" />
        </el-form-item>
        <el-form-item label="使用次数" prop="sum">
          <el-input-number
            input
            v-model="submitFormdata.sum"
            autocomplete="off"
            :min="1"
          />
        </el-form-item>
        <el-form-item label="重量" prop="kg">
          <el-input v-model="submitFormdata.kg" autocomplete="off" />
        </el-form-item>

        <el-form-item>
          <div style="display: flex; justify-content: flex-end; width: 100%">
            <el-button @click="resetForm()">取消</el-button>
            <el-button type="primary" @click="submitForm()"> 提交 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { userLIst } from "@/api/home/index";
import { ElMessage } from "element-plus";
let list1 = reactive([
  {
    key: 1,
    name: "忍村",
  },
  {
    key: 2,
    name: "火村",
  },
  {
    key: 3,
    name: "土村",
  },
]);

const tableData = ref([]);
const deleteRow = (index) => {
  tableData.value.splice(index, 1);
};
let dialogTittle = ref("");
let dialogVisible = ref(false);
let editRow = (e) => {
  dialogTittle.value = "编辑";
  submitFormdata.value = JSON.parse(JSON.stringify(e));
  dialogVisible.value = true;
};
const onAddItem = () => {
  dialogTittle.value = "新增";
  submitFormdata.value = {};
  dialogVisible.value = true;
};

let ruleFormRef = ref(null);
const rules = reactive({
  time: [{ required: true, message: "请填写时间" }],
  name: [{ required: true, message: "请填写名称" }],
});
let submitFormdata = ref({});
const submitForm = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      console.log("submit!");
      if (dialogTittle.value == "编辑") {
        tableData.value.forEach((item, index) => {
          if (item.code == submitFormdata.value.code) {
            tableData.value[index] = { ...item, ...submitFormdata.value };
          }
        });
        dialogVisible.value = false;
        console.log(tableData.value);
        ElMessage.success("操作成功");
      }
      if (dialogTittle.value == "新增") {
        submitFormdata.value.code = Math.floor(Math.random() * 9000) + 1000;
        dialogVisible.value = false;
        tableData.value.push(submitFormdata.value);
        ElMessage.success("操作成功");
        submitFormdata.value = {};
      }
    } else {
      ElMessage.error("请完成表单");
    }
  });
};

const resetForm = () => {
  submitFormdata.value = {};
  dialogVisible.value = false;
};
let query = ref({ name: "" });

onMounted(() => {
  tableData.value = [
    {
      code: "0001",
      name: "足球场",
      status: 60,
      ares: 800,
    },
    {
      code: "0002",
      name: "乒乓球管",
      status: 20,
      ares: 200,
    },
    {
      code: "0003",
      name: "篮球场",
      status: 20,
      ares: 400,
    },
    {
      code: "0004",
      name: "游泳馆",
      status: 20,
      ares: 200,
    },
  ];
});
</script>

<style lang="scss" scoped>
.root {
  width: 100%;
}
.head {
  margin-bottom: 20px;
  .searchItem {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}
</style>
